package com.tinyseed.emcp.api.exception.state;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.tinyseed.emcp.api.exception.entity.ExceptionInfo;
import com.tinyseed.emcp.api.exception.service.ExceptionInfoService;
import com.tinyseed.emcp.api.maintenance.domain.MaintenanceInfo;
import com.tinyseed.emcp.api.maintenance.domain.utlis.MaintenanceInfoFactory;
import com.tinyseed.emcp.api.maintenance.service.MaintenanceService;
import com.tinyseed.emcp.api.maintenance.state.MTNewState;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.beans.Transient;

/**
 * @version Revision 1.0.0
 * @版权：
 * @author: Howard
 * @email:
 * @see:
 * @创建日期：2020/3/13
 * @功能说明：创建异常类
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2020/3/13 wuxiran        创建
 * @end
 */
@NoArgsConstructor
public class NewState extends ExceptionState {

    public final static String NEW_STATE_NAME = "新建";
    public final static String NEW_STATE_CODE = "NEW";

    @Setter
    private MaintenanceService maintenanceService;

    @Setter
    private ExceptionInfoService exceptionInfoService;

    @Setter
    private MaintenanceInfo maintenanceSmallInfo;
    public NewState(ExceptionInfoService exceptionInfoService, MaintenanceService maintenanceService) {
        this.exceptionInfoService = exceptionInfoService;
        this.maintenanceService = maintenanceService;
    }

    @Override
    @Transient
    public void handleException(ExceptionInfo context) {

    }

    @Override
    public void nextStep(ExceptionInfo context) {
        //第一步，生成维保信息类
        MaintenanceInfo maintenanceInfo = MaintenanceInfoFactory.create(context);
        BeanUtil.copyProperties(maintenanceSmallInfo, maintenanceInfo, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));

        maintenanceInfo.setMtStatusCode(MTNewState.NEW_STATE_CODE);
        maintenanceInfo.setMtStatus(MTNewState.NEW_STATE_NAME);

        //保存维保信息
        maintenanceService.save(maintenanceInfo);
        ProcessIngState processIngState = new ProcessIngState();
        context.setCurrentStatus(processIngState.getStateName());
        context.setCurrentStatusCode(processIngState.getStateCode());

        //更改状态
        exceptionInfoService.saveExceptionInfo(context);

    }
    @Override
    public void prevStep(ExceptionInfo context) {
        throw new RuntimeException("没有之前的状态");
    }

    @Override
    public String getStateName() {
        return NEW_STATE_NAME;
    }

    @Override
    public String getStateCode() {
        return NEW_STATE_CODE;
    }
}
